/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.service;

import br.ufc.quixada.adrs.dao.PrazoDAO;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.model.Prazo;
import br.ufc.quixada.adrs.util.Msg;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Escritorio projetos
 */
public class PrazoService {

    public static final Comparator comparadorPorFim = new Comparator<Prazo>() {

        public int compare(Prazo p1, Prazo p2) {
            return p1.getFim().compareTo(p2.getFim());
        }
    };
    private final PrazoDAO pDao = new PrazoDAO();

    public boolean insertPrazo(Prazo prazo) {
        try {
            pDao.insert(prazo);
            return true;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return false;
        }
    }

    public boolean updatePrazo(Prazo prazo) {
        try {
            pDao.update(prazo);
            return true;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return false;
        }
    }

    public boolean updateConfirmacao(Prazo prazo, Adrs adrs, boolean confirmacao) {
        Map confirmacaoMap = new HashMap(3);
        confirmacaoMap.put("confirmado", confirmacao);
        confirmacaoMap.put("prazo", prazo.getId());
        confirmacaoMap.put("adrs", adrs.getId());
        try {
            pDao.updateConfirmacao(confirmacaoMap);
            return true;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return false;
        }
    }

    public boolean deletePrazo(Prazo prazo) {
        try {
            pDao.delete(prazo);
            return true;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return false;
        }
    }

    public Long countPrazosSuperioresAD(Date date) {
        Long nPrazo = null;
        try {
            nPrazo = pDao.getPrazosIniciaisSuperioresAData(date);
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
        }
        return nPrazo;
    }

    public boolean isConfirmado(Prazo prazo, Adrs adrs) {
        Map confirmacaoMap = new HashMap(2);
        confirmacaoMap.put("prazo", prazo.getId());
        confirmacaoMap.put("adrs", adrs.getId());
        try {
            Boolean confirmado = pDao.getConfirmacao(confirmacaoMap);
            if (confirmado == null || !confirmado) {
                return false;
            }
            return true;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return false;
        }
    }

    public Prazo getPrazoById(Long id) {
        try {
            Prazo prazo = pDao.getById(id);
            return prazo;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return null;
        }
    }

    public List<Prazo> getAllPrazos() {
        try {
            List<Prazo> prazos = pDao.getAll();
            return prazos;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return null;
        }
    }

    public List<Prazo> getPrazosSuperioresAData(Date data) {
        try {
            List<Prazo> prazos = pDao.getPrazosDeFimSuperiorAData(data);
            return prazos;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return null;
        }
    }

    public List<Prazo> getPrazosByAdrs(Adrs adrs) {
        try {
            List<Prazo> prazos = pDao.getByAdrs(adrs.getId());
            return prazos;
        } catch (SQLException ex) {
            LoggerFactory.getLogger(this.getClass()).error(Msg.EXCEPTION_MESSAGE, this.getClass().getSimpleName(), ex);
            return null;
        }
    }
}
